//
// Created by yangchao on 2022/4/22.
// 13. 罗马数字转整数: https://leetcode-cn.com/problems/roman-to-integer/
//

#include <string>
using namespace std;

//把一个小值放在大值得左边就是做减法，反之做加法
class RomanToInt {
public:
    int romanToInt(string s) {
        if (s.size() < 1) return 0;
        int res = 0;
        int preVal = getInt(s[0]);
        for (int i = 1; i < s.size(); ++i) {
            int val = getInt(s[i]);
            if (preVal < val) {
                res -= preVal;
            } else {
                res += preVal;
            }
            preVal = val;
        }
        res += preVal;
        return res;
    }

private:
    int getInt(char ch) {
        switch (ch) {
            case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
            default: return 0;
        }
    }
};
